/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConnection.java
 *
 * Created on 2011-05-18, 15:57:46
 */

package pckGestionHoraire;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import jxl.write.WriteException;
import pckFiles.HoraireExcel;
import pckSQL.SQL_Etudiant;
import pckTaxe.IniFile;

/**
 *
 * @author Portable-Maison
 */
public class FrmConnection extends javax.swing.JFrame {

    SQL_Etudiant gaEtudiant;
    
    /** Creates new form FrmConnection */
    public FrmConnection() {
        initComponents();
        positionnerFenetre();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlInformation = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txfCodeUsager = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txfMotPasse = new javax.swing.JTextField();
        pnlAction = new javax.swing.JPanel();
        btnInscription = new javax.swing.JButton();
        btnConnection = new javax.swing.JButton();
        btnFermer = new javax.swing.JButton();
        btnTester = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setTitle("Connection");
        setMinimumSize(new java.awt.Dimension(425, 150));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnlInformation.setMaximumSize(new java.awt.Dimension(500, 60));
        pnlInformation.setMinimumSize(new java.awt.Dimension(300, 50));
        pnlInformation.setPreferredSize(new java.awt.Dimension(300, 60));
        pnlInformation.setLayout(new java.awt.GridLayout(4, 1, 12, 12));

        jLabel1.setDisplayedMnemonic('u');
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setLabelFor(txfCodeUsager);
        jLabel1.setText("  Matricule :");
        pnlInformation.add(jLabel1);
        jLabel1.getAccessibleContext().setAccessibleName("   Matricule :");

        pnlInformation.add(txfCodeUsager);

        jLabel2.setDisplayedMnemonic('m');
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setLabelFor(txfMotPasse);
        jLabel2.setText("  Mot de passe :");
        pnlInformation.add(jLabel2);
        pnlInformation.add(txfMotPasse);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 1;
        getContentPane().add(pnlInformation, gridBagConstraints);

        pnlAction.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlAction.setMaximumSize(new java.awt.Dimension(200, 40));
        pnlAction.setMinimumSize(new java.awt.Dimension(200, 40));
        pnlAction.setPreferredSize(new java.awt.Dimension(200, 40));

        btnInscription.setMnemonic('I');
        btnInscription.setText("Inscription");
        btnInscription.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInscriptionActionPerformed(evt);
            }
        });
        pnlAction.add(btnInscription);

        btnConnection.setMnemonic('C');
        btnConnection.setText("Connection");
        btnConnection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectionActionPerformed(evt);
            }
        });
        pnlAction.add(btnConnection);

        btnFermer.setMnemonic('F');
        btnFermer.setText("Fermer");
        btnFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFermerActionPerformed(evt);
            }
        });
        pnlAction.add(btnFermer);

        btnTester.setText("?");
        btnTester.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTesterActionPerformed(evt);
            }
        });
        pnlAction.add(btnTester);

        jButton1.setText("-");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        pnlAction.add(jButton1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(pnlAction, gridBagConstraints);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pckImage/logoHome.gif"))); // NOI18N
        jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel3.setMaximumSize(new java.awt.Dimension(150, 150));
        jLabel3.setMinimumSize(new java.awt.Dimension(150, 150));
        jLabel3.setPreferredSize(new java.awt.Dimension(150, 150));
        getContentPane().add(jLabel3, new java.awt.GridBagConstraints());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFermerActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnFermerActionPerformed
    {//GEN-HEADEREND:event_btnFermerActionPerformed
    System.exit (0);
    }//GEN-LAST:event_btnFermerActionPerformed

    private void btnInscriptionActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnInscriptionActionPerformed
    {//GEN-HEADEREND:event_btnInscriptionActionPerformed
    FrmInscription laDlg = new FrmInscription();
    laDlg.setLocationRelativeTo(this);
    laDlg.setVisible (true);
    if (!laDlg.isVisible ())
        {
        laDlg.dispose ();
        }
    }//GEN-LAST:event_btnInscriptionActionPerformed

    private void btnTesterActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnTesterActionPerformed
    {//GEN-HEADEREND:event_btnTesterActionPerformed
    FrmInfoConnection laConnection = new FrmInfoConnection();
    laConnection.setLocationRelativeTo(this);
    laConnection.setVisible (true);
    if (!laConnection.isVisible ())
        {
        laConnection.dispose ();
        }
    }//GEN-LAST:event_btnTesterActionPerformed

    private void btnConnectionActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnConnectionActionPerformed
    {//GEN-HEADEREND:event_btnConnectionActionPerformed
    chargerInformation();
    int liEtat;
    liEtat = gaEtudiant.validerConnection (txfCodeUsager.getText().trim (),
                                            txfMotPasse.getText ().trim ());
    if (liEtat > -1)
        {
        FrmFenPrincipale laPrincipale = new FrmFenPrincipale();
        laPrincipale.setLocationRelativeTo(this);
        laPrincipale.setIndex (liEtat);
        this.setVisible (false);
        laPrincipale.setVisible (true);
        }
    else
        {
        JOptionPane.showMessageDialog (this, "Matricule ou mot de passe invalide.");
        }
    }//GEN-LAST:event_btnConnectionActionPerformed

    private void jButton1ActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        try {
            HoraireExcel laHoraire = new HoraireExcel ("Mathieu B");
            laHoraire.finalize ();
        }
        catch (WriteException ex) {
            Logger.getLogger (HoraireExcel.class.getName()).log (Level.SEVERE, null, ex);
        }
        catch (Exception ex) {
            Logger.getLogger (FrmConnection.class.getName()).log (Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void chargerInformation()
    {
    IniFile.lectureFichier ();
    String lsAdresse = IniFile.getValeurSection (0);
    String lsTablename = IniFile.getValeurSection (2);
    if (IniFile.getSize () > 0)
        {
        gaEtudiant = new SQL_Etudiant(lsTablename, lsAdresse, "Info", "Info");
        }
    else
        {
        JOptionPane.showMessageDialog (this, "Aucune connection n'a été initialiser.");
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmConnection().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConnection;
    private javax.swing.JButton btnFermer;
    private javax.swing.JButton btnInscription;
    private javax.swing.JButton btnTester;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel pnlAction;
    private javax.swing.JPanel pnlInformation;
    private javax.swing.JTextField txfCodeUsager;
    private javax.swing.JTextField txfMotPasse;
    // End of variables declaration//GEN-END:variables
    private void positionnerFenetre ()
    {
        Dimension laTailleEcran =
                          Toolkit.getDefaultToolkit ().getScreenSize ();
        Dimension laTailleFenetre = this.getSize ();
        if (laTailleFenetre.height > laTailleEcran.height)
        {
            laTailleFenetre.height = laTailleEcran.height;
        }
        if (laTailleFenetre.width > laTailleEcran.width)
        {
            laTailleFenetre.width = laTailleEcran.width;
        }
        this.setLocation((laTailleEcran.width - laTailleFenetre.width) / 2,
                        (laTailleEcran.height - laTailleFenetre.height) / 2);
    }

}
